<template>
  <div class="pages brandbrandClass" id="app">
    <!-- 弹窗 -->
    <!-- 操作框 -->
    <div class="tableSearch">
      <el-form label-width="100px" class="demo-ruleForm">
        <el-form-item label="" label-width="15px">
          <el-input class="input" v-model="selectdata.orgKey" size="small" placeholder="请输入商户名称"></el-input>
        </el-form-item>
        <el-form-item label="" label-width="15px">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="searchSelectOrg" class="Button">查询</el-button>
        </el-form-item>
        <el-form-item label="" label-width="15px">
          <el-button type="primary" @click="cancel()" size="mini" v-if="unallocates"> 批量取消分配</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="shopList" ref="multipleTable" max-height="300" @selection-change="changeFun">
      <el-table-column type="selection" width="55" @selection-change="changeFun"></el-table-column>
      <el-table-column type="index" label="序号" width="50"></el-table-column>
      <el-table-column property="name" label="门店名称" align="center"></el-table-column>
      <el-table-column property="shopCode" label="编号" align="center"></el-table-column>
      <el-table-column v-if="unallocates" prop="name" align="center" label="操作" width="280">
        <template slot-scope="scope">
          <el-button size="small" @click="cancel(scope.row.id)" type="text" class="addDetail">取消分配</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageblock">
      <el-button size="small" class="topage" type="primary" @click="toPages">确定</el-button>
      <el-pagination @size-change="handleSizeChanges" @current-change="handleCurrentChanges" :current-page="currentPages" :page-sizes="[10,20,30,50]" :page-size="pageSizes" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <div class="operationBox StoreBranch">
      <el-button type="primary" @click="distribution" size="medium" v-if="!unallocates" style="margin-right:10px;">立即分配</el-button>
      <el-button plain @click="Sub" size="medium">返回</el-button>
    </div>
  </div>
</template>
<script>
import { mapState } from "vuex";
import { listShops, allocatedListShop, allotIntegralRule } from "@/api";
import util from "@/util";
export default {
  computed: mapState(["merchantId", "power"]),
  props: {
    dialogVisible: "",
    integralid: "",
    unallocate: "",
    dividetype: "",
    integralMulti: {
      type: Array,
      default: [],
      required: true
    }
  },
  data() {
    return {
      deployId: "",
      unallocates: this.unallocate,
      dialogVisibles: this.dialogVisible,
      dividetypes: this.dividetype, //积分规则类型
      integralids: this.integralid, //积分规则id
      dis: false,
      selectdata: {
        keyword: ""
      },
      shopList: [],
      integralMultis: this.integralMulti,
      // 分页开始
      total: 0,
      pageSizes: 20,
      totalItemss: 0,
      currentPages: 1,
      ids: ""
    };
  },
  watch: {
    unallocate: function(val) {
      this.unallocates = val;
      console.log(val);
      if (this.unallocates == false) {
        this.tableget_integral(20, 1);
      } else {
        this.tableget_See(20, 1);
      }
    },
    integralMulti: function(val) {
      console.log(val);
      this.integralMultis = val;
    },
    integralid: function(val) {
      console.log(val);
      this.integralids = val;
    },
    dividetype: function(val) {
      console.log(val);
      this.dividetypes = val;
    }
  },
  created() {
    // 获取品牌分类名称列表
  },
  mounted() {
    if (this.unallocates == false) {
      this.tableget_integral(20, 1);
    } else {
      this.tableget_See(20, 1);
    }
  },
  methods: {
    cancel(id) {
      this.$confirm("确定要取消对本门店的分配吗？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消"
        // type: "warning"
      })
        .then(() => {
          let Data = {
            integralDiscountRuleId: this.integralids,
            shopIds: ids
          };
          if (this.dividetypes == true) {
            allotIntegralRule(data).then(res => {
              this.$message({
                type: "success",
                message: "取消分配成功!"
              });
              this.Sub();
            });
          } else {
            allotIntegralDiscountRule(data).then(res => {
              this.$message({
                type: "success",
                message: "取消分配成功!"
              });
              this.Sub();
            });
          }
        })
        .catch(() => {});
    },
    Sub() {
      this.$emit("dialogVis", false);
    },
    //分配商户多选
    changeFun(val) {
      this.isshop = true;
      this.shopMulti = val;
    },
    tableget_integral(pageSize, pageNum) {
      let Data = {
        pageNum: pageNum,
        pageSize: pageSize,
        shopId: this.merchantId
      };
      listShops(Data).then(res => {
        this.total = res.data.pageTotal;
        this.shopList = res.data.list;
        let aress = [];
      });
    },
    tableget_See(pageSize, pageNum) {
      let Data = {
        pageNum: pageNum,
        pageSize: pageSize,
        shopId: this.merchantId
      };
      allocatedListShop(Data).then(res => {
        this.total = res.data.pageTotal;
        this.shopList = res.data.listShopDetailVOs;
        let aress = [];
      });
    },
    searchSelectOrg() {
      //门店查询
      this.tableget_integral(this.pageSizes, 1);
    },
    //分配商户
    distribution() {
      var arr = [];
      var arrs = [];
      for (var i = 0; i < this.integralMulti.length; i++) {
        arr.push(this.integralMulti[i].id);
      }
      for (var i = 0; i < this.shopMulti.length; i++) {
        arrs.push(this.shopMulti[i].id);
      }
      let data = {
        goodsIds: arrs.toString(),
        shopIds: arr.toString()
      };
      if (this.dividetypes == true) {
        allotIntegralRule(data).then(res => {
          this.$message({
            type: "success",
            message: "分配成功!"
          });
          this.Sub();
        });
      } else {
        allotIntegralDiscountRule(data).then(res => {
          this.$message({
            type: "success",
            message: "分配成功!"
          });
          this.Sub();
        });
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //分配下级组织机构-----------分页
    toPages() {
      if (this.unallocates == false) {
        this.tableget_integral(this.pageSizes, this.currentPages);
      } else {
        this.tableget_See(this.pageSizes, this.currentPages);
      }
    },
    handleSizeChanges(val) {
      this.pageSizes = val;
      if (this.unallocates == false) {
        this.tableget_integral(this.pageSizes, this.currentPages);
      } else {
        this.tableget_See(this.pageSizes, this.currentPages);
      } // console.log(`每页 ${val} 条`);
    },
    handleCurrentChanges(val) {
      this.currentPages = val;
      if (this.unallocates == false) {
        this.tableget_integral(this.pageSizes, this.currentPages);
      } else {
        this.tableget_See(this.pageSizes, this.currentPages);
      }
    }
  }
};
</script>
<style scoped>
.showmore {
  display: block;
}
#app {
  text-align: left;
}
.StoreBranch {
  width: 200px;
  margin: 10px auto;
  margin-top: 20px;
}
</style>

